package org.iori.pdf;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class TestShuKaShi {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws DocumentException, IOException{
		// TODO Auto-generated method stub
		//元のpdfファイル
		String pdf_source_file="C:/Users/founder/Desktop/テスト.pdf";
		//すかし化完了のpdfファイル
		String pdf_shukashi_file="C:/Users/founder/Desktop/テスト_すかし.pdf";
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(pdf_shukashi_file)));
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		//pdfファイルをすかし化に処理
		setShuKaShimark(bos,pdf_source_file,format.format(cal.getTime()) + "  ダウンロード用：" + "毎日", 16);
	}
	public static void setShuKaShimark(BufferedOutputStream bos, String input,
			 String shuKaShiName, int permission)
			 throws DocumentException, IOException {
		 PdfReader reader = new PdfReader(input);
		 PdfStamper stamper = new PdfStamper(reader, bos);
		 int total = reader.getNumberOfPages() + 1;
		 PdfContentByte content;
		 //UniGB-UCS2-H
		 BaseFont base = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED);
		 PdfGState gs = new PdfGState();
		 for (int i = 1; i < total; i++) {
			 content = stamper.getOverContent(i);//コンテンツのヘッダですかしを追加
			 //content = stamper.getUnderContent(i);//コンテンツのフットですかしを追加
			 gs.setFillOpacity(0.2f);
			 // content.setGState(gs);
			 content.beginText();
			 content.setColorFill(BaseColor.LIGHT_GRAY);
			 content.setFontAndSize(base, 50);
			 content.setTextMatrix(70, 200);
			 content.showTextAligned(Element.ALIGN_CENTER, "毎日新聞社！", 300, 350, 55);
//			 content.setColorFill(Color.BLACK);
			 content.setFontAndSize(base, 8);
			 content.showTextAligned(Element.ALIGN_CENTER, "ダウンロード時間：" + shuKaShiName + "", 300, 10, 0);
			 content.endText();
		 }
		 stamper.close();
	}
}